﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Printing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using MDV.Frameworks;
using MDV.Entities;
using MDV.Business;
using System.IO;
using System.Security.Cryptography;


namespace MDV
{
    public partial class frmMain : Form
    {
        #region variables
        Common common = new Common();
        BusinessObject bo = new BusinessObject();
        private int btnVisible;
        Search search = new Search();
        private PrintDocument printDocument1 = new PrintDocument();
        PrintDialog PrintDialog1 = new PrintDialog();
        PrintDocument docToPrint = new PrintDocument();
        DataTable history = new DataTable();
        float sizeOfDictionary = 12;
        public static int returnfontSize;
        public static int returnTextFont;
        public static int applyCheck=0;
        private Boolean helpShow = false;
        private ToolTip toolTip = new ToolTip();
        //int openSetting=0;
        #endregion

        public frmMain()
        {
            InitializeComponent();
            sbtnHelp.ToolTip = common.tipbBtHelp;
            TreeDesease.Visible = false;
            //lblCheck.Text = rgSearch.SelectedIndex.ToString();
           
            showList(0); //show List Medicines ,0 : medicine , 1,-1 : thanh phan

            // Associate the PrintPage event handler with the PrintPage event.
            docToPrint.PrintPage +=
                new PrintPageEventHandler(document_PrintPage);
            history.Columns.Add("Name", typeof(string));
            history.Columns.Add("Type", typeof(string));
            this.txtSearch.KeyPress += new System.Windows.Forms.KeyPressEventHandler(CheckKeys);
            this.txtGoogle.KeyPress += new System.Windows.Forms.KeyPressEventHandler(CheckKeys);
            //SetToolTip();
            SetTree();
        }

        private void CheckKeys(object sender, System.Windows.Forms.KeyPressEventArgs e)
        {
            //txtSearch.Focus();
           
            if (e.KeyChar == (char)13 )
            {
                // Then Enter key was pressed
                
                
                if (_checkSearch == true)
                {
                    //MessageBox.Show(_checkFirstUseSearch.ToString());
                    SearchCondition();
                    //txtSearch.SelectAll();
                    if (txtSearch.Text.IndexOf(";") <= 0)
                        ShowDataRichTextBox();
                }
                
                //MessageBox.Show(_checkFirstUseSearch.ToString() + _checkFirstUseGoogle.ToString());
                if (_checkGoogle == true)
                {
                    try
                    {
                        if (txtGoogle.Text != "" && _checkFirstUseGoogle == true)
                            Business.SearchByGoogle.GoogleSearch(txtGoogle.Text, _checkFirstUseGoogle);
                    }
                    catch (Win32Exception noBrowser)
                    {
                        if (noBrowser.ErrorCode == -2147467259)
                            MessageBox.Show(noBrowser.Message);
                    }
                    catch (Exception other)
                    {
                        MessageBox.Show(other.Message);
                    }
                }
            } 
        }
        private void showList(int index) {
            lbAlphabet.DisplayMember = bo.display(index).Columns[0].ColumnName;
            lbAlphabet.ValueMember = bo.display(index).Columns[0].ColumnName;
            lbAlphabet.DataSource = bo.display(index); 
        }

        private void sbtnHistory_Click(object sender, EventArgs e)
        {
            if (btnVisible == 0)
            {
                sbtnClearAll.Visible = true;
                sbtnDelete.Visible = true;
                btnVisible = 1;
                lblTable.Visible = false;

                lbAlphabet.DisplayMember = history.Columns[0].ColumnName;
                lbAlphabet.ValueMember = history.Columns[0].ColumnName;
                lbAlphabet.DataSource = history;
                HistoryOrNot = true;
            }
            else
            {
                sbtnClearAll.Visible = false;
                sbtnDelete.Visible = false;
                btnVisible = 0;
                lblTable.Visible = true;
                HistoryOrNot = false;
                showList(rgSearch.SelectedIndex);
            }

        }
        
        private void btUpdate_Click(object sender, EventArgs e)
        {
            OpenFileDialog dialogUpdate = new OpenFileDialog();
            dialogUpdate.Filter = "Text Files (*.txt)|*.txt|Word Documents" +
                                     "(*.doc)|*.doc|All Files (*.*)|*.*";
            dialogUpdate.ShowReadOnly = true;
            dialogUpdate.Multiselect = true;
            dialogUpdate.Title = "Open files for custom application";
            dialogUpdate.ShowDialog();

            if (MessageBox.Show("Are you sure to update?", "", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK)
            {
                //lblCheck.Text = Path.GetFileName(dialogUpdate.FileName);
                Business.Update update = new Business.Update();
                //lblUpdate.Text = update.ReadFile(dialogUpdate.FileName); //show ra query
                update.UpdateOnDB(dialogUpdate.FileName);
                //lblCheck.Text = update.check;
            }
            else {
                //lblCheck.Text = "chua nhan dc file";
            }
            dialogUpdate.Dispose();
            
        }
        
        private void sbtnGo_Click(object sender, EventArgs e)
        {
            try
            {
            //    txtSearch.Select(0,txtSearch.TextLength);
            //    MessageBox.Show(txtSearch.SelectedText);
                if (_checkFirstUseSearch == true)
                {
                    SearchCondition();
                    if (txtSearch.Text.IndexOf(";") <= 0)
                        ShowDataRichTextBox();
                }
                txtSearch.SelectAll();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private bool HistoryOrNot = false;
        private void lbAlphabet_Click(object sender, EventArgs e)
        {
            _checkFirstUseSearch = true;
            ShowDataRichTextBox();
        }
        private void ShowDataRichTextBox ()
        {
            try
            {
                txtSearch.Text = lbAlphabet.SelectedValue.ToString();
                txtSearch.SelectAll();
                if (HistoryOrNot == true)
                {
                    if (history.Rows[lbAlphabet.SelectedIndex][1].ToString() == "0")
                        rgSearch.SelectedIndex = 0;
                    else
                        rgSearch.SelectedIndex = 1;
                }
                else {
                    try
                    {
                        //DataRow dtRow = new DataRow();
                        DataRow dtRow = history.NewRow();
                        dtRow["Name"] = lbAlphabet.SelectedValue;
                        dtRow["Type"] = rgSearch.SelectedIndex;
                        //lblCheck.Text = "nothing";
                        Boolean check = true;
                        if (dtRow["Name"] != null && dtRow["Type"] != null)
                        {
                            if (history.Rows.Count == 0)
                            {
                                history.Rows.Add(dtRow);
                            }
                            else
                            {
                                for (int i = 0; i < history.Rows.Count; i++)
                                {
                                    if (history.Rows[i][0].ToString() == dtRow["Name"].ToString())
                                        check = false;
                                }
                                if(check)
                                    history.Rows.Add(dtRow);
                            }
                        }
                        
                    }
                    catch (Exception e) {
                       MessageBox.Show(e.ToString());
                    }
                }
                
                rtbDictionary.Clear();
                var dt = new DataTable();
                string tblname, column;
                string que = lbAlphabet.SelectedValue.ToString();
                var da = new SqlDataAdapter();
                if (rgSearch.SelectedIndex == 0) { tblname = "Medicines"; column = "[Medicine Name]"; }
                else
                {
                    tblname = "Component"; column = "[Component Name]";
                }
                string conStr = "Server=localhost; Integrated Security=SSPI;database=Medicines";
                var con = new SqlConnection(conStr);
                con.Open();

                if (tblname == "Medicines")
                {
                    var command = new SqlCommand();
                    command.Connection = con;
                    command.CommandType = CommandType.Text;
                    command.CommandText = @"Select * from " + tblname + " Where " + column + "=N" + "'" + que + "'";
                    da.SelectCommand = command;
                    da.Fill(dt);

                    var command1 = new SqlCommand();
                    command1.Connection = con;
                    command1.CommandType = CommandType.Text;
                    var dt1 = new DataTable();
                    var da1 = new SqlDataAdapter();

                    command1.CommandText = @"SELECT [Component Name] FROM Medicines JOIN Component
                                            ON Medicines.[Medicine ID]=Component.[Medicine ID]
                                             WHERE Medicines.[Medicine ID] =  '" + dt.Rows[0][0].ToString() + "'";
                    da1.SelectCommand = command1;
                    da1.Fill(dt1);


                    string ecipient = "";
                    int i = 0;
                    while (i < dt1.Rows.Count)
                    {
                        ecipient += dt1.Rows[i][0] + "\n";
                        i++;
                    }
                   
                    rtbDictionary.Select(0, 7); 
                    rtbDictionary.SelectionFont = new Font(MDV.GUI.FormSetting.fontFamily, MDV.GUI.FormSetting.fontSize, FontStyle.Bold);
                    rtbDictionary.SelectionColor = Color.Blue;
                    rtbDictionary.SelectedText = "Excipient:\n";
                    rtbDictionary.SelectionFont = new Font(MDV.GUI.FormSetting.fontFamily, MDV.GUI.FormSetting.fontSize-1);
                    rtbDictionary.SelectedText = ecipient;
                    rtbDictionary.SelectionFont = new Font(MDV.GUI.FormSetting.fontFamily, MDV.GUI.FormSetting.fontSize, FontStyle.Bold);
                    rtbDictionary.SelectionColor = Color.Blue;
              
                    rtbDictionary.SelectionFont = new Font(MDV.GUI.FormSetting.fontFamily, MDV.GUI.FormSetting.fontSize, FontStyle.Bold);
                    rtbDictionary.SelectionColor = Color.Blue;
                    rtbDictionary.SelectedText = "\nProprietary Medicine:\n";
                    rtbDictionary.SelectionFont = new Font(MDV.GUI.FormSetting.fontFamily, MDV.GUI.FormSetting.fontSize-1);
                    rtbDictionary.SelectedText = dt.Rows[0][2].ToString();
                    rtbDictionary.SelectionFont = new Font(MDV.GUI.FormSetting.fontFamily, MDV.GUI.FormSetting.fontSize, FontStyle.Bold);
                    rtbDictionary.SelectionColor = Color.Blue;
                    rtbDictionary.SelectedText = "\n\nAction:\n";
                    rtbDictionary.SelectionFont = new Font(MDV.GUI.FormSetting.fontFamily, MDV.GUI.FormSetting.fontSize-1);
                    rtbDictionary.SelectedText = dt.Rows[0][3].ToString();
                    rtbDictionary.SelectionFont = new Font(MDV.GUI.FormSetting.fontFamily, MDV.GUI.FormSetting.fontSize, FontStyle.Bold);
                    rtbDictionary.SelectionColor = Color.Blue;
                    rtbDictionary.SelectedText = "\n\nIndication:\n";
                    rtbDictionary.SelectionFont = new Font(MDV.GUI.FormSetting.fontFamily, MDV.GUI.FormSetting.fontSize - 1);
                    rtbDictionary.SelectedText = dt.Rows[0][4].ToString();
                    rtbDictionary.SelectionFont = new Font(MDV.GUI.FormSetting.fontFamily, MDV.GUI.FormSetting.fontSize, FontStyle.Bold);
                    rtbDictionary.SelectionColor = Color.Blue;
                    rtbDictionary.SelectedText = "\n\nDose Use:\n";
                    rtbDictionary.SelectionFont = new Font(MDV.GUI.FormSetting.fontFamily, MDV.GUI.FormSetting.fontSize - 1);
                    rtbDictionary.SelectedText = dt.Rows[0][5].ToString();
                    rtbDictionary.SelectionFont = new Font(MDV.GUI.FormSetting.fontFamily, MDV.GUI.FormSetting.fontSize, FontStyle.Bold);
                    rtbDictionary.SelectionColor = Color.Blue;
                    rtbDictionary.SelectedText = "\n\nContraindication:\n";
                    rtbDictionary.SelectionFont = new Font(MDV.GUI.FormSetting.fontFamily, MDV.GUI.FormSetting.fontSize - 1);
                    rtbDictionary.SelectedText = dt.Rows[0][6].ToString();
                    rtbDictionary.SelectionFont = new Font(MDV.GUI.FormSetting.fontFamily, MDV.GUI.FormSetting.fontSize, FontStyle.Bold);
                    rtbDictionary.SelectionColor = Color.Blue;
                    rtbDictionary.SelectedText = "\n\nAttention:\n";
                    rtbDictionary.SelectionFont = new Font(MDV.GUI.FormSetting.fontFamily, MDV.GUI.FormSetting.fontSize - 1);
                    rtbDictionary.SelectedText = dt.Rows[0][7].ToString();
                    rtbDictionary.SelectionFont = new Font(MDV.GUI.FormSetting.fontFamily, MDV.GUI.FormSetting.fontSize, FontStyle.Bold);
                    rtbDictionary.SelectionColor = Color.Blue;
                    rtbDictionary.SelectedText = "\n\nMedicine Form:\n";
                    rtbDictionary.SelectionFont = new Font(MDV.GUI.FormSetting.fontFamily, MDV.GUI.FormSetting.fontSize - 1);
                    rtbDictionary.SelectedText = dt.Rows[0][8].ToString();
                }

                else
                {
                    var command3 = new SqlCommand();
                    command3.Connection = con;
                    command3.CommandType = CommandType.Text;
                    var dt1 = new DataTable();
                    var da1 = new SqlDataAdapter();
                    string queComponent = lbAlphabet.SelectedValue.ToString();
                    command3.CommandText = @"SELECT * FROM Component JOIN Medicines ON Component.[Medicine ID]=Medicines.[Medicine ID]
                                             WHERE " + column + "=" + "'" + queComponent + "'";
                    da1.SelectCommand = command3;
                    da1.Fill(dt1);

                    string ecipient = "";

                    int i = 0;
                    while (i < dt1.Rows.Count)
                    {
                        ecipient += "In " + dt1.Rows[i][5] + " : " + dt1.Rows[i][2] + "\n";
                        i++;
                    }
                   
                    //rtbDictionary.Text = ecipient;

                    rtbDictionary.SelectionFont = new Font(MDV.GUI.FormSetting.fontFamily, MDV.GUI.FormSetting.fontSize, FontStyle.Bold);
                    rtbDictionary.SelectionColor = Color.Blue;
                    rtbDictionary.SelectedText = "Excipient Name:\n";
                    rtbDictionary.SelectionFont = new Font(MDV.GUI.FormSetting.fontFamily, MDV.GUI.FormSetting.fontSize-1);
                    rtbDictionary.SelectedText = dt1.Rows[0][3].ToString();
                    rtbDictionary.SelectionFont = new Font(MDV.GUI.FormSetting.fontFamily, MDV.GUI.FormSetting.fontSize, FontStyle.Bold);
                    rtbDictionary.SelectionColor = Color.Blue;
                    rtbDictionary.SelectedText = "\n\nDose use:\n";
                    rtbDictionary.SelectionFont = new Font(MDV.GUI.FormSetting.fontFamily, MDV.GUI.FormSetting.fontSize-1);
                    rtbDictionary.SelectedText = ecipient;

                    if (lbAlphabet.SelectedItems.Count > 1)
                    {
                        var command4 = new SqlCommand();
                        command4.Connection = con;
                        command4.CommandType = CommandType.Text;
                        var dt2 = new DataTable();
                        var da2 = new SqlDataAdapter();
                    
                        command4.CommandText = @"SELECT   FROM Component JOIN Medicines ON Component.[Medicine ID]=Medicines.[Medicine ID]
                                             WHERE " + column + "=" + "'" + queComponent + "'";
                        da2.SelectCommand = command4;  
                    }

                }
            }
            catch (Exception ex)
            {
                //MessageBox.Show(ex.ToString());
            }
        }     

        private void txtSearch_TextChanged(object sender, EventArgs e)
        {
            if (txtSearch.Text == " ")
            {
                txtSearch.Text = "";
                // ShowDataRichTextBox();
            }
            
            SupportSearch(txtSearch.Text);
 
        }
        private void SupportSearch(string str)
        {
            try
            {
                // Set our intial index variable to -1.
                int x = -1;
                // If the search string is empty exit.
                if (str.Length != 0)
                {
                    // Loop through and find each item that matches the search string.
                    do
                    {
                        // Retrieve the item based on the previous index found. Starts with -1 which searches start.
                        x = lbAlphabet.FindString(str, x);
                        // If no item is found that matches exit.
                        if (x != -1)
                        {
                            // Since the FindString loops infinitely, determine if we found first item again and exit.
                            if (lbAlphabet.SelectedIndices.Count > 0)
                            {
                                if (x == lbAlphabet.SelectedIndices[0])
                                    return;
                            }
                            // Select the item in the ListBox once it is found.
                            lbAlphabet.SetSelected(x, true);
                            if (lbAlphabet.TopIndex != lbAlphabet.SelectedIndex)
                                // Make the currently selected item the top item in the ListBox.
                                lbAlphabet.TopIndex = lbAlphabet.SelectedIndex;
                        }
                    } while (x != -1);
                    
                }
            }
            catch (Exception ex) { MessageBox.Show(ex.ToString()); 
            }
        }
        private void SearchCondition()
        {
            txtSearch.SelectAll();
            int numberrow=0;
            int i = txtSearch.Text.IndexOf(";");
            int space = txtSearch.Text.IndexOf(" ");
            
            while (space == 0)
            {
                txtSearch.Text = txtSearch.Text.Remove(0, space+1);
                space = txtSearch.Text.IndexOf(" ");
            }
            string strForListbox="";
            if (space > 0)
            {
                strForListbox = txtSearch.Text.Substring(0, space);
                SupportSearch(strForListbox);
            }
            if (rgSearch.SelectedIndex > 0 && i > 0)
            {
                lbAlphabet.DisplayMember = search.GoSearch(txtSearch.Text, rgSearch.SelectedIndex).Columns[0].ColumnName;
                lbAlphabet.ValueMember = search.GoSearch(txtSearch.Text, rgSearch.SelectedIndex).Columns[0].ColumnName;
                lbAlphabet.DataSource = search.GoSearch(txtSearch.Text, rgSearch.SelectedIndex);
                var dtExpicient = bo.DisplayRichText(txtSearch.Text);
                rtbDictionary.Clear();
                while (numberrow != dtExpicient.Rows.Count)
                {
                    //rtbDictionary.AppendText("Excipient : " + dtExpicient.Rows[numberrow][1] + "\n" +
                    //                            "Medicine : " + dtExpicient.Rows[numberrow][0] + "\n" +
                    //                            "Dose : " + dtExpicient.Rows[numberrow][2] + "\n\n\n");
                    rtbDictionary.SelectionFont = new Font("Arial", 10, FontStyle.Bold);
                    rtbDictionary.SelectionColor = Color.Blue;
                    rtbDictionary.SelectedText = "Excipient Name:";
                    rtbDictionary.SelectionFont = new Font("Arial", 10);
                    rtbDictionary.SelectionColor = Color.Black;
                    rtbDictionary.SelectedText = dtExpicient.Rows[numberrow][1].ToString();
                    rtbDictionary.SelectionFont = new Font("Arial", 10, FontStyle.Bold);
                    rtbDictionary.SelectionColor = Color.Blue;
                    rtbDictionary.SelectedText = "\nIN Medicine:";
                    rtbDictionary.SelectionFont = new Font("Arial", 10);
                    rtbDictionary.SelectionColor = Color.Black;
                    rtbDictionary.SelectedText = dtExpicient.Rows[numberrow][0].ToString();
                    rtbDictionary.SelectionFont = new Font("Arial", 10, FontStyle.Bold);
                    rtbDictionary.SelectionColor = Color.Blue;
                    rtbDictionary.SelectedText = "\nDose:";
                    rtbDictionary.SelectionFont = new Font("Arial", 10);
                    rtbDictionary.SelectionColor = Color.Black;
                    rtbDictionary.SelectedText = dtExpicient.Rows[numberrow][2].ToString()+"\n\n";
                    numberrow++;
                }
          
            }
            else
            {
                SupportSearch(txtSearch.Text);
            }
        }

        private void rgSearch_SelectedIndexChanged(object sender, EventArgs e)
        {
            HistoryOrNot = false;
            sbtnClearAll.Visible = false;
            sbtnDelete.Visible = false;
            btnVisible = 0;
            lblTable.Visible = true;
            HistoryOrNot = false;
            showList(rgSearch.SelectedIndex);


            if (rgSearch.SelectedIndex == 1)
            {
                lblTable.Text = "Excipients";
                sbtnAdd.Visible = true;
                lbAlphabet.SelectionMode = SelectionMode.One;
            }
            else
            {
                lblTable.Text = "Medicines";
                sbtnAdd.Visible = false;
                lbAlphabet.SelectionMode =SelectionMode.One;
            }
        }

        private void lblTable_Click(object sender, EventArgs e)
        {
            if (rgSearch.SelectedIndex == 0)
            {
                rgSearch.SelectedIndex = 1;
            }
            else
            {
                rgSearch.SelectedIndex = 0;
            }
        }

        private void sbtnHelp_Click(object sender, EventArgs e)
        {
            MDV.GUI.FormHelp frmHelp = new GUI.FormHelp();
            frmHelp.Show();
            if (helpShow == false)
            {
                setToolTip_on();
                helpShow = true;
            }
            else if (helpShow == true)
            {
                setToolTip_off();
                helpShow = false;
            }
        }
        
        private void sbtnSetting_Click(object sender, EventArgs e)
        {
            MDV.GUI.FormSetting setting; 
            //if (openSetting == 0)
            //{
                setting = new GUI.FormSetting();
            //    openSetting++;
            //}
            //else
            //    setting = new GUI.FormSetting(returnTextFont, returnfontSize);
            setting.Show();
        }

        private void sbtnPrint_Click(object sender, EventArgs e)
        {
            
            PrintDialog1.AllowSomePages = true;
            PrintDialog1.ShowHelp = true;
            PrintDialog1.Document = docToPrint;

            DialogResult result = PrintDialog1.ShowDialog();

            // If the result is OK then print the document.
            if (result == DialogResult.OK)
            {
                docToPrint.Print();
            }
        }

        private void document_PrintPage(object sender,
        System.Drawing.Printing.PrintPageEventArgs e)
        {

            string stringtoprint = rtbDictionary.Text;
            System.Drawing.Font printFont = new System.Drawing.Font
                ("Arial", 15, System.Drawing.FontStyle.Regular);

            // Draw the content.
            e.Graphics.DrawString(stringtoprint, printFont,
                System.Drawing.Brushes.Black, 10, 10);
        }

        private void sbtnClearAll_Click(object sender, EventArgs e)
        {
            history.Rows.Clear();
        }

        private void sbtnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                if (history.Rows.Count == 0)
                {
                    //lblCheck.Text = "ko co ban ghi";
                }
                else
                {
                    history.Rows[lbAlphabet.SelectedIndex].Delete();
                }
            }
            catch (Exception ex) {
                //lblCheck.Text = "loi~ history";
            }
        }

        private bool _checkFirstUseSearch = false;
        private bool _checkFirstUseGoogle= false;
        private bool _checkSearch = false;
        private bool _checkGoogle = false;

        private void txtSearch_Click(object sender, EventArgs e)
        {
            txtSearch.SelectAll();
            _checkSearch = true;
            _checkGoogle = false;
            if (_checkFirstUseSearch == true)
            {}
            else
            {
                txtSearch.Text = "";
                _checkFirstUseSearch = true;
                //_checkFirstUseGoogle = false;
            }
                
        }

        private void txtGoogle_Click(object sender, EventArgs e)
        {

            txtGoogle.SelectAll();
            _checkGoogle = true;
            _checkSearch = false;
            if (_checkFirstUseGoogle == true)
            { }
            else
            {
                txtGoogle.Text = "";
                _checkFirstUseGoogle = true;
            }
        }

        private void sbtnGoogle_Click(object sender, EventArgs e)
        {
            //Use no more than one assignment when you test this code. 
            try
            {
                if (txtGoogle.Text != "" && _checkFirstUseGoogle == true)
                    Business.SearchByGoogle.GoogleSearch(txtGoogle.Text, _checkFirstUseGoogle);
            }
            catch(Win32Exception noBrowser)
            {
                if (noBrowser.ErrorCode == -2147467259)
                    MessageBox.Show(noBrowser.Message);
            }
            catch (Exception other)
            {
                MessageBox.Show(other.Message);
            }
        }


        private void frmMain_Click(object sender, EventArgs e)
        {
            txtSearch.Focus();
        }

        private void lbAlphabet_KeyPress(object sender, KeyPressEventArgs e)
        {

            if (e.KeyChar == (Char)13)
            {
                ShowDataRichTextBox();

            }
            else
            {
                txtSearch.Clear();
            }
            txtSearch.Focus();
        
            if (e.KeyChar >= 65 && e.KeyChar <= 122)
            {
                txtSearch.AppendText(e.KeyChar.ToString());
                _checkSearch = true;
            }
        }

        private void rtbDictionary_KeyPress(object sender, KeyPressEventArgs e)
        {
            txtSearch.Clear();
            txtSearch.Focus();
            if (e.KeyChar >= 65 && e.KeyChar <= 122)
            {
                txtSearch.AppendText(e.KeyChar.ToString());
                _checkSearch = true;
            }
        }

        private void setToolTip_on() 
        {
            toolTip.SetToolTip(txtSearch, "search");
            toolTip.SetToolTip(txtGoogle, "google");
            toolTip.SetToolTip(rtbDictionary, "dictionary");
            toolTip.SetToolTip(lbAlphabet, "list box");
            sbtnHistory.ToolTip = "history";
            sbtnPrint.ToolTip = "Print";
            sbtnSetting.ToolTip = "Setting";
            sbtnHelp.ToolTip = "Help";
            sbtnGo.ToolTip = "GO";
            sbtnGoogle.ToolTip = "Google";
            sbtnClearAll.ToolTip = "ClearAll";
            sbtnDelete.ToolTip = "Delete";
        }

        private void setToolTip_off()
        {
            toolTip.SetToolTip(txtSearch, "");
            toolTip.SetToolTip(txtGoogle, "");
            toolTip.SetToolTip(rtbDictionary, "");
            toolTip.SetToolTip(lbAlphabet, "");
            sbtnHistory.ToolTip = "";
            sbtnPrint.ToolTip = "";
            sbtnSetting.ToolTip = "";
            sbtnHelp.ToolTip = "";
            sbtnGo.ToolTip = "";
            sbtnGoogle.ToolTip = "";
            sbtnClearAll.ToolTip = "";
            sbtnDelete.ToolTip = "";
        }

        private void sbtnAdd_Click(object sender, EventArgs e)
        {
            String str_selectedValue = lbAlphabet.SelectedValue.ToString();
            String str_search = txtSearch.Text;
            if (str_search != null)
            {
                txtSearch.Text = str_search + ";" + str_selectedValue;
            }
        }

  

        private void tcMain_SelectedPageChanged(object sender, DevExpress.XtraTab.TabPageChangedEventArgs e)
        {
            if (tcMain.SelectedTabPage.Name == "xtDictionary") {
                lbAlphabet.Visible = true;
                TreeDesease.Visible = false;
            }
            if (tcMain.SelectedTabPage.Name == "xtDisease")
            {
                TreeDesease.Visible = true;
                lbAlphabet.Visible = false;
            }
           
        }

        private void SetTree() 
        {
            TreeDesease.Nodes.Clear();
            TreeDesease.Width = 200;
            DataTable cutDuplicateDT = DuplicateDatable(bo.TreeDisease());
            
            foreach (DataRow dr in cutDuplicateDT.Rows)
            {
                TreeNode tnParent = new TreeNode();
                TreeDesease.ItemHeight = 20;
                Font boldFont = new Font("Arial", 10, FontStyle.Bold | FontStyle.Underline);
                tnParent.NodeFont = boldFont;
                tnParent.Text = dr["Type"].ToString()+" ";
                TreeDesease.Nodes.Add(tnParent);
          
                foreach (DataRow dr2 in bo.TreeDisease().Rows)
                {

                    if (dr2["Type"].ToString().Equals(dr["Type"].ToString()))
                    {              
                        tnParent.Nodes.Add(dr2["Disease"].ToString());  
                    }

                }
             
            }

       }

        

        // Xoa trung lap trong Datatable
        private DataTable DuplicateDatable(DataTable dt) 
        {
            DataTable DuplicateDT = dt;
            // create a dv from the source dt 
            DataView dv = new DataView(DuplicateDT);
            // set the output columns array of the destination dt 
            string[] strColumns = { "Type" };
            // true = yes, i need distinct values. 
            DuplicateDT = dv.ToTable(true, strColumns);
            return DuplicateDT;
        }

     

      

        private void TreeDesease_AfterSelect(object sender, TreeViewEventArgs e)
        {
            try
            {

                if (TreeDesease.SelectedNode.Level > 0)
                {

                    rtbDisease.Clear();
                    foreach (DataRow dr2 in bo.TreeDisease().Rows)
                    {
                        if (TreeDesease.SelectedNode.Text.Equals(dr2["Disease"].ToString()))
                        {
                            rtbDisease.AppendText(dr2["Content"].ToString());

                        }
                    }
                }


            }
            catch
            {
                // MessageBox.Show("Khong the in ra du lieu");
            }
        }

        
    }
}

